Ext.define('AM.view.baseData.List', {
    extend: 'AM.default.DefaultList',
    alias: ['widget.basedatalist'],
    requires: ['AM.default.DefaultActiveStateCombo', "AM.default.DefaultSearchForm"],
    
    searchDataTypeID: null,
    initComponent: function(){
    	this.title = baseData_lang.baseDataFrame_listTitle;
    	this.columns = [{
            header: baseData_lang.columns_dataName,
            dataIndex: 't.dataName',
            flex: 1,
            renderer: function(value, metaData, record){
                metaData.tdAttr = 'data-qtip="' + value + '"';
                return value;
            }
        }, {
            header: baseData_lang.columns_dataCode,
            dataIndex: 't.dataCode',
            flex: 1
        }, {
            header: baseData_lang.columns_activeState,
            dataIndex: 't.activeState',
            renderer: function(v){
                if (v == 2) {
                    return '<span style="color:red;">' + baseData_lang.columns_activeStateText[1] + '</span>';
                }
                else {
                    return '<span style="color:green;">' + baseData_lang.columns_activeStateText[0] + '</span>';
                }
            },
            width: 80
        }, {
            header: baseData_lang.columns_orderNum,
            dataIndex: 't.orderNum',
            width: 50
        }];
        this.store = Ext.create('AM.store.baseData.BaseDataList');
        
        this.callParent(arguments);
    },
    
    onAddClick: function(button){
        var typeID = this.searchDataTypeID;
        if (typeID == null) {
            Ext.Msg.alert(orchid_lang.promptWindowInfo.warningTitle, baseData_lang.editFormInfo.addWarningMsg);
        }
        else {
            var win = Ext.create('AM.view.baseData.Edit', {
                grid: this,
                title: baseData_lang.editFormInfo.addTitle
            }).show();
            win.down('form').getForm().findField("searchDataTypeID").setValue(typeID);
        }
    },
    
    onUpdateClick: function(button){
    	var me = this;
        var record = me.getSelectionModel().getSelection()[0];
        var win = Ext.create('AM.view.baseData.Edit', {
            notEdit: false,
            grid: me,
            title: baseData_lang.editFormInfo.updateTitle
        }).show();
        win.down('form').getForm().load({
//            url: "module/basedata/preUpdateBaseData.json?searchDataId=" + record.get("dataID")
			url: me.store.perUpdateUrl,
			params: {
				searchDataId: record.get("dataID")
			}
        });
        
    },
    
    onReuseClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("t.activeState") == 1) {
                Ext.Msg.show({
                    title: orchid_lang.promptWindowInfo.warningTitle,
                    msg: orchid_lang.promptWindowInfo.reuseWarningMsg,
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
        Ext.getCmp("loadingMask").show();
        Ext.Ajax.request({
            url: me.store.tokenUrl,
            success: function(response){
                Ext.getCmp("loadingMask").hide();
                var data = response.responseText;
                var token = Ext.decode(data, true);
                Ext.Msg.confirm({
                    title: orchid_lang.promptWindowInfo.promptTitle,
                    msg: orchid_lang.promptWindowInfo.reusePromptText,
                    buttons: Ext.Msg.OKCANCEL,
                    fn: function(id){
                        if (id == 'ok') {
                            Ext.getCmp("loadingMask").show();
                            var baseDataIds = new Array();
                            Ext.Array.each(records, function(name, index){
                                baseDataIds.push(name.get("dataID"));
                            });
                            Ext.Ajax.request({
                                url: me.store.reuseUrl,
                                params: {
                                    searchActive: 1,
                                    baseDataIds: baseDataIds,
                                    "springmvc.TOKEN": token
                                },
                                success: function(response){
                                    Ext.getCmp("loadingMask").hide();
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    if (json.success) {
//                                        Ext.Msg.alert('消息', json.msg, function(){
                                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.reuseSuccessText, function(){
                                            var store = me.getStore();
                                            store.reload();
                                        });
                                    }
                                    else {
                                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.reuseFailureText, function(){
                                            var store = me.getStore();
                                            store.reload();
                                        });
                                    }
                                }
                            });
                        }
                        else {
                            return;
                        }
                    }
                });
            }
        });
    },
    
    onDiscardClick: function(button){
        var me = this;
        var records = this.getSelectionModel().getSelection();
        
        for (var i = 0; i < records.length; i++) {
            if (records[i].get("t.activeState") == 2) {
                Ext.Msg.show({
                    title: orchid_lang.promptWindowInfo.warningTitle,
                    msg: orchid_lang.promptWindowInfo.discardWarningMsg,
                    icon: Ext.Msg.WARNING,
                    buttons: Ext.Msg.OK
                });
                return;
            }
        }
		Ext.getCmp("loadingMask").show();
		Ext.Ajax.request({
    	  	url: me.store.tokenUrl,
            success: function(response){
                Ext.getCmp("loadingMask").hide();
                var data = response.responseText;
                var token = Ext.decode(data, true);
                Ext.Msg.confirm({
                    title: orchid_lang.promptWindowInfo.promptTitle,
                    msg: orchid_lang.promptWindowInfo.discardPromptText,
                    buttons: Ext.Msg.OKCANCEL,
                    fn: function(id){
                        if (id == 'ok') {
                            Ext.getCmp("loadingMask").show();
                            var baseDataIds = new Array();
                            Ext.Array.each(records, function(name, index){
                                baseDataIds.push(name.get("dataID"));
                            });
                            Ext.Ajax.request({
								url: me.store.discardUrl,
                                params: {
                                    searchActive: 2,
                                    baseDataIds: baseDataIds,
                                    "springmvc.TOKEN": token
                                },
                                success: function(response){
                                    Ext.getCmp("loadingMask").hide();
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    if (json.success) {
//                                        Ext.Msg.alert('消息', json.msg, function(){
                                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.discardSuccessText, function(){
                                            var store = me.getStore();
                                            store.reload();
                                        });
                                    }
                                    else {
                                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, orchid_lang.promptWindowInfo.discardFailureText, function(){
                                            var store = me.getStore();
                                            store.reload();
                                        });
                                    }
                                }
                            });
                        }
                        else {
                            return;
                        }
                    }
                });
            }
        });
    },
    onSynClick: function(){
    	var me = this;
        Ext.getCmp("loadingMask").show();
        Ext.Ajax.request({
      	  	url: me.store.tokenUrl,
            success: function(response){
                Ext.getCmp("loadingMask").hide();
                var data = response.responseText;
                var token = Ext.decode(data, true);
                Ext.Msg.confirm({
                    title: orchid_lang.promptWindowInfo.promptTitle,
                    msg: baseData_lang.synInfo.synPromptMsg,
                    buttons: Ext.Msg.OKCANCEL,
                    fn: function(id){
                        if (id == 'ok') {
                            Ext.getCmp("loadingMask").show();
                            Ext.Ajax.request({
									url: me.store.synBaseDataCacheUrl,
                                params: {
                                    "springmvc.TOKEN": token
                                },
                                success: function(response){
                                    Ext.getCmp("loadingMask").hide();
                                    var data = response.responseText;
                                    var json = Ext.decode(data, true);
                                    if (!json.success && !json.msg) {
                                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, baseData_lang.synInfo.synFailureText, function(){
                                            var store = me.getStore();
                                            store.reload();
                                        });
                                    }
                                    else {
                                        Ext.Msg.alert(orchid_lang.promptWindowInfo.newsTitle, json.msg, function(){
                                            var store = me.getStore();
                                            store.reload();
                                        });
                                    }
                                }
                            });
                        }
                        else {
                            return;
                        }
                    }
                });
            }
        });
    },
    //导出学时数据
    onExportClick : function() {
    	var me = this;
    	Ext.create("AM.view.baseData.Export", {
    		title : "导出学时数据",
    		height : 140,
    		width : 350,
    		grid : me
    	}).show();
    },
  //上传学时数据
	onImportClick: function(){
		var me = this;
		Ext.create("AM.view.baseData.Upload", {
			title: "上传学时数据",
			height: 140,
			width: 350,
			grid: me
		}).show();
	  },
    listeners: {
        viewready: function(){
            this.addDocked([{
                dock: 'top',
                xtype: "searchform",
                items: [{
                        xtype: "textfield",
                        name: "searchBaseName",
                        labelAlign: 'right',
                        labelWidth: 70,
                        emptyText: baseData_lang.searchFormInfo.searchBaseName_emptyText,
                        fieldLabel: baseData_lang.searchFormInfo.searchBaseName
                    },{
                        xtype: "textfield",
                        name: "searchDataCode",
                        labelAlign: 'right',
                        labelWidth: 70,
                        emptyText: baseData_lang.searchFormInfo.searchDataCode_emptyText,
                        fieldLabel: baseData_lang.searchFormInfo.searchDataCode
                    },{
                        xtype: 'activeCombo',
                        name: "searchActiveState",
                        labelWidth: 70,
                        defaultActive: true
                    }],
                onResetClick: function(button){
                    var form = button.up('form');
                    var gridPanel = form.grid;
                    var newParams = {
                        searchDataTypeID: gridPanel.searchDataTypeID,
                        searchActiveState: 1
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    
                    // 重置查询
                    gridPanel.down("activeCombo").reset();
                    // 4.0强制更改store参数写法
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    
                    form.getForm().reset();
                },
                grid: this
            }, {
                xtype: 'toolbar',
                dock: 'top',
                displayInfo: true,
                items: [this.addButton, "-", this.updateButton, "-", this.reuseButton, "-", this.discardButton, "-", {
                    text: baseData_lang.tbarButton[0],
                    iconCls: 'refurbishIco',
                    itemId: this.oldId + "Syn",
                    scope: this,
                    handler: this.onSynClick
                },"-",{
                    text: '导出学时数据',
                    iconCls: 'deriveIco',
                    scope: this,
                    handler: this.onExportClick
				},"-",{
                    text: '上传学时数据',
                    iconCls: 'batchImportRegistrationIco',
                    scope: this,
                    handler: this.onImportClick
				}, "->", "->", this.countSelectPanel]
            }]);
            Ext.getCmp("loadingMask").hide();
            // 触发权限过滤事件
            this.fireEvent("permission", this);
        }
    }
});
